import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export interface ColorizeTask {
  id: string;
  originalImage: string;
  coloredImage?: string;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  params: any;
  createdAt: string;
  completedAt?: string;
  error?: string;
}

interface ColorizeStore {
  tasks: ColorizeTask[];
  addTask: (task: Omit<ColorizeTask, 'id' | 'createdAt'>) => void;
  updateTask: (id: string, updates: Partial<ColorizeTask>) => void;
  removeTask: (id: string) => void;
  clearCompleted: () => void;
}

export const useColorizeStore = create<ColorizeStore>()(
  persist(
    (set) => ({
      tasks: [],
      
      addTask: (task) =>
        set((state) => ({
          tasks: [
            ...state.tasks,
            {
              ...task,
              id: task.id || Math.random().toString(36).substr(2, 9),
              createdAt: new Date().toISOString(),
            },
          ],
        })),
      
      updateTask: (id, updates) =>
        set((state) => ({
          tasks: state.tasks.map((task) =>
            task.id === id ? { ...task, ...updates } : task
          ),
        })),
      
      removeTask: (id) =>
        set((state) => ({
          tasks: state.tasks.filter((task) => task.id !== id),
        })),
      
      clearCompleted: () =>
        set((state) => ({
          tasks: state.tasks.filter(
            (task) => task.status !== 'completed' && task.status !== 'failed'
          ),
        })),
    }),
    {
      name: 'colorize-store',
    }
  )
);
